import 'package:dio/dio.dart';
import 'package:dio_proxy_plugin/dio_proxy_plugin.dart';
import 'package:flutter/services.dart';
import 'package:flutter_base/utils/log/utils_log.dart';

abstract class HttpProxy {
  /// flutter 添加可抓包功能
  static void setProxy(Dio dio) async {
    String deviceProxy;
    try {
      deviceProxy = await DioProxyPlugin.deviceProxy;
    } on PlatformException {
      deviceProxy = '';
      clog('Failed to get system proxy.');
    }
    if (deviceProxy.isNotEmpty) {
      var arrProxy = deviceProxy.split(':');
      final port = int.tryParse(arrProxy[1]) ?? 8888;
      var httpProxyAdapter = HttpProxyAdapter(ipAddr: arrProxy[0], port: port);
      dio.httpClientAdapter = httpProxyAdapter;
    }
  }
}
